package com.zhangsheng.imserver.netty;

import com.zhangsheng.contant.IMRedisKey;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PreDestroy;

@Slf4j
@Component
@AllArgsConstructor
public class IMServerGroup implements CommandLineRunner {

    public static volatile long serverId = 0;

    RedisTemplate<String, Object> redisTemplate;

    private final IMServer imServer;

    /***
     * 判断服务器是否就绪
     *
     * @return
     **/
    public boolean isReady() {
        return imServer.isReady();
    }

    @Override
    public void run(String... args) throws Exception {
        // 初始化SERVER_ID
        serverId = redisTemplate.opsForValue().increment(IMRedisKey.IM_MAX_SERVER_ID, 1);
        // 启动服务
        imServer.start();
    }

    @PreDestroy
    public void destroy() {
        // 停止服务
        imServer.stop();
    }
}
